<?php

namespace app\admin\controller\projects\content;

use app\Base;
use app\service\admin\projects\content\ArticleService;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\response\Json;
use think\response\View;

/**
 * Class Article
 * @package projects\controller\admin\content
 * @author Du
 * @date 2021/4/1
 * @time 17:47
 */
class Article extends Base
{
    /**
     * 首页
     * @param ArticleService $service
     * @return View
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     * @author Du
     * @date 2021/4/1 18:06
     */
    public function show(ArticleService $service): View
    {
        $res = $service::getClassify();
        return view('projects/content/article', ['data' => $res]);
    }

    /**
     * 获取文章列表
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     * @author Du
     * @date 2021/4/1 18:06
     */
    public function getList(ArticleService $service): Json
    {
        $res = $service::getList($this->params);
        $this->res['count'] = $res['count'];
        $this->res['data'] = $res['data'];
        return $this->returnData();
    }

    /**
     * 详情
     * @param ArticleService $service
     * @return Json
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function info(ArticleService $service): Json
    {
        $res = $service::info($this->params);
        if (!$res) {
            return $this->returnData('noData');
        }
        $this->res['data'] = $res;
        return $this->returnData();
    }

    /**
     * 添加
     * @param ArticleService $service
     * @return Json
     */
    public function add(ArticleService $service): Json
    {
        $res = $service::add($this->params);
        if (!$res) {
            return $this->returnData('addFail');
        }
        return $this->returnData();
    }

    /**
     * 修改
     * @param ArticleService $service
     * @return Json
     * @throws DbException
     */
    public function update(ArticleService $service): Json
    {
        $res = $service::update($this->params);
        if (!$res) {
            return $this->returnData('updateFail');
        }

        return $this->returnData();
    }

    /**
     * 删除
     * @param ArticleService $service
     * @return Json
     * @throws DbException
     */
    public function delete(ArticleService $service): Json
    {
        $res = $service::delete($this->params);
        if (!$res) {
            return $this->returnData('deleteFail');
        }
        return $this->returnData();
    }
}